/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.bbcnet.awesomeplayer;
import entagged.audioformats.AudioFile;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author eburgs
 */
 
public class Sync extends JFrame implements ActionListener {
    
    XML xml = XML.getInstance();
    public File tmp2 = null;
    public JFrame frame;
    public JComboBox lists;
    JButton OK = new JButton("OK");
    JButton Abb = new JButton("Abbrechen");
    /**
     * Opens a Menu, where you can Synchronize your Playlists with a Directory
     */
    
  public Sync() {

      //Buttons, Panels, AL,Fenster
        frame = new JFrame();
        JPanel panel = new JPanel(new BorderLayout());      
        OK.addActionListener(this);
        
        panel.add(OK,BorderLayout.PAGE_END);
        frame.setSize(200,100);
        frame.setLocation(300,150);
        frame.setTitle("Synchro");
        frame.setResizable(false);
        
    
        
        //Auswahlliste JCombobox
        String[] playlists = new String[xml.getPlaylists().length];
        
        for(int i = 0; i<xml.getPlaylists().length;i++){
            playlists[i] = xml.getPlaylists()[i].toString();
        }
        
        lists = new JComboBox(playlists);
        lists.setSelectedIndex(0);
        lists.addActionListener(this);
        
        panel.add(lists,BorderLayout.PAGE_START);
        frame.add(panel);
        Image img = new ImageIcon(getClass().getResource("/ch/bbcnet/awesomeplayer/files/logo2.png")).getImage();
        frame.setIconImage(img);        
        frame.setVisible(true);
  }
  /**
   * 
   * @param in1
   * @param out1
   * @throws IOException <br />
   * Copy Files from File1 to File2
   */
  //Kopiert ein File von der Quelle zum Ziel
  public static void copyFile(File in1, File out1) throws IOException { 
        
      //Pfad wird richtig bearbeitet
        File f = new File(out1.getAbsolutePath()+"/"+in1.getName());
        f.createNewFile();
        File out = new File(out1.getAbsolutePath()+"/"+in1.getName());
        
        //Kopiert von Ch1 zu Ch2
        FileChannel inChannel = new FileInputStream(in1).getChannel(); 
        FileChannel outChannel = new FileOutputStream(out).getChannel(); 
        try { 
            inChannel.transferTo(0, inChannel.size(), outChannel); 
        } catch (IOException e) { 
            throw e; 
        } finally { 
            if (inChannel != null) 
                inChannel.close(); 
            if (outChannel != null) 
                outChannel.close(); 
        } 
    } 
  


    @Override
    public void actionPerformed(ActionEvent e) {
        
        //Wenn OK-Knopf gedrückt wird 
        if(e.getSource()==OK){
        Object[][] var = xml.getData(lists.getSelectedItem().toString());
        frame.dispose();
        
        //Datum Einstellen, für Ordner
        Date dt = new Date();
        SimpleDateFormat df = new SimpleDateFormat( "dd-MM-yyyy" );
        df.setTimeZone( TimeZone.getDefault() );         
        
        if(Gui.datentraeger==false){
        // JFileChooser-Objekt erstellen
        JFileChooser chooser2 = new JFileChooser();
        chooser2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser2.setMultiSelectionEnabled(true);
        
        // Dialog zum Auswählen des Pfads der Liste
        chooser2.showDialog(null, "Zielverzeichnis Wählen");
        
        //Erstellt neuen Ordner in Zielverzeichnis
        

        tmp2 = chooser2.getSelectedFile();
        }
        else{
            File file[] = File.listRoots();
            for (int i=0; i < file.length; i++) {
 
            try {
                sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder(file[i]);
                if(sf.getFolderType().equals("Wechseldatenträger")){
                    tmp2 = sf.getAbsoluteFile();
                    break;
                }
            } catch (Exception es) {
                //JOptionPane.showMessageDialog(null, "Ein Fehler ist aufgetreten, keinen Wechseldatenträger erkannt");
            }
        }
            
        }
        File cheat = new File(tmp2+"/Playlist-"+df.format(dt));
        File[] tm = new File[var.length];
        cheat.mkdir();
        try {
            this.setCursor(new Cursor(3));
            Thread.sleep(4000);
            this.setCursor(new Cursor(0));
        } catch (InterruptedException ex) {
            
        }
        
        
        //Kopiert Files in einer For-Schleife
        for(int j = 0; j<var.length;j++){
            tm[j] = new File (var[j][4].toString());
            tm[j] = tm[j].getAbsoluteFile();
                try {
                    copyFile(tm[j],cheat);
                } catch (IOException ex) {
                    
                }
        }
        
   //Schliessen
  }else{
            this.dispose();
        }

    }

  

}
